var _api = _interopRequireDefault(require("../../utils/api")), _apiCode = _interopRequireDefault(require("../../utils/api-code"));

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

function _slicedToArray(t, e) {
    return _arrayWithHoles(t) || _iterableToArrayLimit(t, e) || _nonIterableRest();
}

function _nonIterableRest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance");
}

function _iterableToArrayLimit(t, e) {
    var a = [], r = !0, i = !1, n = void 0;
    try {
        for (var o, s = t[Symbol.iterator](); !(r = (o = s.next()).done) && (a.push(o.value), 
        !e || a.length !== e); r = !0) ;
    } catch (t) {
        i = !0, n = t;
    } finally {
        try {
            r || null == s.return || s.return();
        } finally {
            if (i) throw n;
        }
    }
    return a;
}

function _arrayWithHoles(t) {
    if (Array.isArray(t)) return t;
}

var cache = {}, searchName = "", EMAIL_DOMAINS = [ "qq.com", "gmail.com", "126.com", "163.com", "hotmail.com" ], SEARCH_ITEMS = {
    corpname: {
        api: _api.default.receipt.searchsuggest
    },
    position: {
        api: _api.default.weappcard.searchposition
    },
    email: {
        api: function(t) {
            var e = _slicedToArray(t.keyword.split("@"), 2), a = e[0], r = e[1], i = void 0 === r ? "" : r, n = EMAIL_DOMAINS.filter(function(t) {
                return t.includes(i);
            }).map(function(t) {
                return a + "@" + t;
            });
            return Promise.resolve({
                code: _apiCode.default.SUCCESS,
                data: {
                    lists: n
                }
            });
        }
    }
};

Component({
    properties: {
        value: {
            type: String,
            value: "",
            observer: "_valueChange"
        },
        maxNum: {
            type: Number,
            value: 4
        },
        mask: {
            type: Boolean,
            value: !0
        },
        markstyle: String,
        name: String,
        direction: {
            type: String,
            value: "bottom"
        },
        selector: {
            type: String,
            value: ""
        }
    },
    data: {
        searchList: [],
        first: !0
    },
    ready: function() {
        cache[this.data.name] = this;
    },
    methods: {
        getPosition: function() {
            var e = this, t = this.data, a = t.selector, r = t.markstyle;
            a && !r && wx.createSelectorQuery().select(a).boundingClientRect(function(t) {
                t && e.setData({
                    markstyle: "top: ".concat(t.top + t.height, "px; bottom: 0")
                });
            }).exec();
        },
        onSelect: function(t) {
            var e = t.currentTarget.dataset.index;
            this.setData({
                first: !0
            });
            var a = this.data, r = a.name, i = a.searchList;
            this.triggerEvent("select", {
                name: r,
                value: i[e].text
            }, {}), this.onHide();
        },
        onHide: function() {
            this.setData({
                searchList: []
            });
        },
        clearOtherList: function() {
            var e = this.data.name;
            Object.keys(cache).filter(function(t) {
                return t !== e;
            }).filter(function(t) {
                return cache[t];
            }).forEach(function(t) {
                var e = cache[t];
                if (e && e.data) {
                    var a = e.data.searchList;
                    clearTimeout(e.timer), a.length && e.setData({
                        searchList: []
                    });
                }
            });
        },
        _valueChange: function(t, e) {
            var a = this.data, r = a.first, i = a.name;
            return r && t ? this.setData({
                first: !1
            }) : (this.setData({
                first: !1
            }), t ? (searchName = i, this.clearOtherList(), void this.request(t, e, i)) : this.onHide());
        },
        request: function(a, t, r) {
            var i = this;
            a !== t && (clearTimeout(this.timer), this.timer = wx.setTimeout(function() {
                var t = SEARCH_ITEMS[r];
                t && t.api({
                    keyword: a
                }).then(function(t) {
                    if (t.code === _apiCode.default.SUCCESS && a === i.data.value && r === searchName) {
                        var e = i.formatList(a, t.data.lists);
                        i.setData({
                            searchList: e
                        }), e.length && i.getPosition();
                    }
                });
            }, 500));
        },
        formatList: function(a, t) {
            return t.map(function(t) {
                return {
                    text: t,
                    lists: t.split(a).reduce(function(t, e) {
                        return t.concat([ {
                            type: 1,
                            text: e
                        }, {
                            type: 0,
                            text: a
                        } ]);
                    }, []).slice(0, -1)
                };
            });
        }
    }
});